<?php
    namespace Home\Controller;
    use  Think\Controller;
    class CommonController extends Controller{
        public function _initialize()
        {
            if (empty($_SESSION['name'])) {
                $this->redirect('Login/index');
            }
        }
        static function sub($str)
        {
            return substr($str,0,4).'/'.substr($str,4,2).'/'.substr($str,6,2).'/';
        }
        public function upload(){
            $upload = new \Think\Upload();
            // 实例化上传类
            $upload->maxSize   =     3145728 ;
            // 设置附件上传大小
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');


            //设置附件上传类型
            $upload->rootPath  = './Public/';
            $upload->savePath  = './Uploads/';
            $upload->saveName  = date('Ymd',time()).uniqid();
            $upload->autoSub   = true;
            $upload->subName   =  date('Y',time()).'/'.date('m',time()).'/'.date('d',time());

            //设置附件上传目录
            //上传文件
            $info = $upload->upload();
            if(!$info){
                $this->error($upload->getError());
            }else{// 上传成功
                //做图片压缩方法
                self::zoom($info['pic']['savename']);
                return $info['pic']['savename'];
            }
        }
        static function zoom($filename)
        {
            $image = new \Think\Image();
            $path='./Public/Uploads/'.self::sub($filename);
            $image->open($path.$filename);
            $image->thumb(250,250)->save($path.'250_'.$filename);
            $image->thumb(150,150)->save($path.'150_'.$filename);
            $image->thumb(300,300)->save($path.'300_'.$filename);
            $image->thumb(80,80)->save($path.'80_'.$filename);
        }
    }